package com.ludomancy.gamelib;

import java.awt.Graphics;
import java.io.DataInputStream;
import java.io.InputStream;

public class Tileset
{
	public int[][] map;
	public int width;
	public int height;
	public ImageStrip tiles;
	
	public Tileset(String file, ImageStrip tiles)
	{
		this.tiles = tiles;
		InputStream is = GameApplet.gameAppletInstance.getClass().getResourceAsStream(file);
		DataInputStream dis = new DataInputStream(is);
		try
		{
			this.width = Integer.parseInt(dis.readLine().trim());
			this.height = Integer.parseInt(dis.readLine().trim());

			map = new int[width][height];
			String mapData = dis.readLine().trim();
			String[] tilesData = mapData.split(",");
			int i = 0;
			for (int y = 0; y < height; y++)
			{
				for (int x = 0; x < width; x++)	
				{
					map[x][y] = Integer.parseInt(tilesData[i++]);
				}
			}
			dis.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void paint(Graphics g)
	{
		for (int y = 0; y < height; y++)
		{
			for (int x = 0; x < width; x++)
			{
				tiles.paint(g, map[x][y], x * tiles.frameWidth, y * tiles.frameHeight);
			}
		}
	}
}
